class_name TorchSwitch
extends Node2D


@export var torch_arr: Array[SceneTorch] = []
var data: TorchSwitchData


func _ready() -> void:
	if not data:
		data = TorchSwitchData.new()
	data.id = self.name


func init_by_data(d: TorchSwitchData):
	self.data = d
	if data.is_on:
		switch()


func take_hit(hit_data: HitData, source: Node2D = null):
	switch()
	

func switch():
	print("switch count")
	scale.y = -scale.y
	
	if scale.y > 0:
		data.is_on = false
		sequence_close_all_torch()
	else:	
		data.is_on = true
		sequence_open_all_torch()
		

func sequence_open_all_torch():
	for torch in torch_arr:
		torch.open()
		await get_tree().create_timer(0.5).timeout
	

func sequence_close_all_torch():
	for torch in torch_arr:
		torch.close()
		await get_tree().create_timer(0.5).timeout
